package cn.dnui_wnx725.dnuifood_16110100725.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.adapter.ShopAdapter;
import cn.dnui_wnx725.dnuifood_16110100725.bean.ShopBean;
import cn.dnui_wnx725.dnuifood_16110100725.controller.FoodActivity;
import cn.dnui_wnx725.dnuifood_16110100725.listener.ListListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.ShopModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private SwipeRefreshLayout mSwipeLayout;


    ListListener<ShopBean> listListener = new ListListener<ShopBean>() {
        @Override
        public void onResponse(final List<ShopBean> list) {
            final ShopAdapter shopAdapter = new ShopAdapter(getActivity(), list, R.layout.item);
            ShopAdapter.OnItemClickListener onItemClickListener = new ShopAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(getActivity(), FoodActivity.class);
                    intent.putExtra("shopid", list.get(position).getShop_id());
                    startActivity(intent);
                }
            };
            shopAdapter.setList(list);
            shopAdapter.setmOnItemClickListener(onItemClickListener);
            recyclerView.setAdapter(shopAdapter);
            mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    shopAdapter.setList(list);
                    shopAdapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };


    @Override
    void initView() {

        recyclerView = view.findViewById(R.id.rc_shop);
        mSwipeLayout = view.findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
    }

    @Override
    void initEvent() {
        ShopModel shopModel = new ShopModel();
        shopModel.getShopMessage(listListener);
    }

    @Override
    void initData() {

    }

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_shop);
        return super.onCreateView(inflater,container,savedInstanceState);
    }
    private void request_list(){
        ShopModel shopModel = new ShopModel();
        shopModel.getShopMessage(listListener);
    }

    @Override
    public void onResume() {
        request_list();
        super.onResume();
    }
}
